#ifndef GAMECLIENTTHREADMODEL_H
#define GAMECLIENTTHREADMODEL_H

#include <QThread>
#include <QDebug>

class GameClientThreadModel: public QThread
{
    Q_OBJECT
    Q_ENUMS(_GameStatus)
    Q_PROPERTY(_GameStatus gameStatus READ gameStatus)

    Q_PROPERTY(bool win READ win)   //是否获胜
    Q_PROPERTY(bool bTerminateGame READ bTerminateGame)//玩家强制结束游戏(扣分显示)
public:
    enum _GameStatus
    {
        Game_Status_Continue = 0,   //继续游戏
        //Game_Status_NextTurn,
        Game_Status_GameOver,       //游戏正常结束
        Game_Status_Exit,           //网络指令 游戏退出(玩家被动)
        Game_Status_Terminated,           //游戏强制退出(玩家主动)
        Game_Status_Err = -1             //游戏出错
    };
public:
    explicit GameClientThreadModel(QObject *parent = NULL);
    virtual ~GameClientThreadModel();

//本类发出的信号
signals:
    void s_InitOnce();   //给qml通知
    void s_GameInit();   //游戏初始化
    void s_GameBegin();  //游戏结束
    void s_GameOver(_GameStatus nRet);      //游戏结束时通知


//给外部发送的信息:
    //void s_RefreshUI();         //刷新UI
    void s_Message(QString msg);                    //游戏中显示信息
    void s_Debug(QString msg);                      //显示调试信息
    void s_Critical(QString msg);                      //显示错误信息

public:
    virtual int InitOnce();   //进行初始化(仅1次,由外部配置好后调用)
    virtual int InitOnceInThread(); //线程中初始化(1次)

    //每次游戏开始前初始化,sr为随机数种子
    virtual int GameInit(int sr = -1);

    //游戏开始调用
    _GameStatus GameBegin();

    //游戏循环(重写)
    virtual _GameStatus GameLoop()
    {
        emit s_Debug("<font size=10>---Made By Pleafles---</font>");

        for(;;) //游戏主循环
        {
            if(gameStatus() != Game_Status_Continue)
                break;
        }
        return setGameStatus(Game_Status_GameOver);
    }

    //游戏结束
    virtual int GameOver();

    //强制结束游戏,尽量调用后加wait()
    Q_INVOKABLE virtual void terminateGame(int ret, _GameStatus status = Game_Status_Exit)
    {
        setGameStatus(status);
        qDebug() << "terminateGame:" << ret;
    }

    //设置游戏状态
    _GameStatus setGameStatus(_GameStatus status)
    {
        if(m_eGameStatus == Game_Status_Continue && status != Game_Status_Continue)
        {
            m_eGameStatus = status;
            emit s_Debug(tr("[Game]游戏将要结束:状态码:%1").arg(m_eGameStatus));
        }
        return m_eGameStatus;
    }

    //检测游戏是否结束,返回Game_Status_Continue或Game_Status_Gameover
    virtual _GameStatus CheckGameOver()
    {
        if(gameStatus() != Game_Status_Continue)return gameStatus();



        //return setGameStatus(Game_Status_GameOver);
        return gameStatus();
    }

/*/其他
public:
    virtual void Message(const QString& msg)
    {
        emit s_Message(msg);
    }

    virtual void Debug(const QString& msg)
    {
        qDebug() << msg;
        emit s_Debug(msg);
    }

    virtual void Critical(const QString& msg)
    {
        qCritical() << msg;
        emit s_Debug(msg);
    }
    //void Sleep(unsigned long msec){msleep(msec);}
*/

/*
public:
//UI游戏框架
    virtual void WaitForUserAction()
    {
        m_waitForUserAction.wait(&m_mutexUserAction);
    }
    virtual void userActionFinished()
    {
        m_waitForUserAction.wakeAll();
    }
*/

//框架属性:
public:
    _GameStatus gameStatus() const
    {
        return m_eGameStatus;
    }

//游戏属性
public:
    bool win() const
    {
        return true;
    }
    bool bTerminateGame() const
    {
        return gameStatus() == Game_Status_Terminated;
    }

protected:
    virtual void run() Q_DECL_OVERRIDE;




//框架数据
private:
    bool m_bInitOnce;   //是否初始化过
    bool m_bInitOnceInThread;   //是否初始化过
public:
    _GameStatus m_eGameStatus;      //游戏状态,结束,继续,强制退出,错误等



public:
//游戏外部设置:
    int         m_nGameRandom;   //游戏随机数(局数)

/* UI游戏框架
    //QWaitCondition需要已锁的才能wait,wait时会闪unlock一下!
    QWaitCondition  m_waitForUserAction;
    QMutex          m_mutexUserAction;
*/
/* 网络游戏框架
    ProducerModel   m_Producer;
    TcpSocket       *pGameSocket;
    bool            netPlay;
*/

};

#endif // GAMECLIENTTHREADMODEL_H
